# 智能排课系统 API 测试脚本
# 目标服务器: 172.16.29.198

$ServerIP = "172.16.29.198"
$BackendPort = 8090
$BaseURL = "http://${ServerIP}:${BackendPort}"

Write-Host "============================================" -ForegroundColor Cyan
Write-Host "智能排课系统 API 测试" -ForegroundColor Cyan
Write-Host "服务器: $ServerIP" -ForegroundColor Cyan
Write-Host "============================================" -ForegroundColor Cyan
Write-Host ""

# 测试1: 网络连通性
Write-Host "测试 1: 检查网络连通性..." -ForegroundColor Yellow
$ping = Test-Connection -ComputerName $ServerIP -Count 2 -Quiet
if ($ping) {
    Write-Host "✅ 服务器可达" -ForegroundColor Green
} else {
    Write-Host "❌ 无法连接到服务器" -ForegroundColor Red
    exit 1
}
Write-Host ""

# 测试2: 端口检测
Write-Host "测试 2: 检查后端端口 $BackendPort..." -ForegroundColor Yellow
$portTest = Test-NetConnection -ComputerName $ServerIP -Port $BackendPort -InformationLevel Quiet
if ($portTest) {
    Write-Host "✅ 端口 $BackendPort 已开放" -ForegroundColor Green
} else {
    Write-Host "❌ 端口 $BackendPort 未开放" -ForegroundColor Red
    Write-Host "   请检查后端服务是否启动或防火墙设置" -ForegroundColor Yellow
    exit 1
}
Write-Host ""

# 测试3: 管理员登录API (错误账号)
Write-Host "测试 3: 测试管理员登录 API (错误账号)..." -ForegroundColor Yellow
try {
    $body = @{
        adminNo = "test"
        adminPassword = "test"
    } | ConvertTo-Json

    $response = Invoke-RestMethod -Uri "$BaseURL/admin/login" `
        -Method POST `
        -ContentType "application/json" `
        -Body $body `
        -ErrorAction Stop

    Write-Host "响应码: 200" -ForegroundColor Green
    Write-Host "响应数据: $($response | ConvertTo-Json)" -ForegroundColor Gray
} catch {
    $statusCode = $_.Exception.Response.StatusCode.value__
    Write-Host "响应码: $statusCode" -ForegroundColor Yellow
    Write-Host "这是正常的，因为测试账号不存在" -ForegroundColor Gray
}
Write-Host ""

# 测试4: 管理员登录API (正确账号)
Write-Host "测试 4: 测试管理员登录 API (正确账号)..." -ForegroundColor Yellow
try {
    $body = @{
        adminNo = "admin"
        adminPassword = "123456"
    } | ConvertTo-Json

    $response = Invoke-RestMethod -Uri "$BaseURL/admin/login" `
        -Method POST `
        -ContentType "application/json" `
        -Body $body `
        -ErrorAction Stop

    Write-Host "✅ 登录成功" -ForegroundColor Green
    Write-Host "管理员信息:" -ForegroundColor Gray
    Write-Host "  账号: $($response.data.adminNo)" -ForegroundColor Gray
    Write-Host "  姓名: $($response.data.adminName)" -ForegroundColor Gray
    Write-Host "  角色: $($response.data.adminRole)" -ForegroundColor Gray
} catch {
    $statusCode = $_.Exception.Response.StatusCode.value__
    Write-Host "❌ 登录失败 (状态码: $statusCode)" -ForegroundColor Red
    Write-Host "错误信息: $($_.Exception.Message)" -ForegroundColor Yellow
}
Write-Host ""

# 测试5: 学生登录API
Write-Host "测试 5: 测试学生登录 API..." -ForegroundColor Yellow
try {
    $body = "stuNo=20230001&password=010011"

    $response = Invoke-RestMethod -Uri "$BaseURL/student-user/login" `
        -Method POST `
        -ContentType "application/x-www-form-urlencoded" `
        -Body $body `
        -ErrorAction Stop

    Write-Host "✅ 学生登录成功" -ForegroundColor Green
    Write-Host "学生信息:" -ForegroundColor Gray
    Write-Host "  学号: $($response.data.stuNo)" -ForegroundColor Gray
    Write-Host "  姓名: $($response.data.stuName)" -ForegroundColor Gray
} catch {
    $statusCode = $_.Exception.Response.StatusCode.value__
    Write-Host "❌ 学生登录失败 (状态码: $statusCode)" -ForegroundColor Red
    Write-Host "错误信息: $($_.Exception.Message)" -ForegroundColor Yellow
}
Write-Host ""

# 测试6: 教师登录API
Write-Host "测试 6: 测试教师登录 API..." -ForegroundColor Yellow
try {
    $body = "teacherNo=T001&password=123456"

    $response = Invoke-RestMethod -Uri "$BaseURL/teacher-user/login" `
        -Method POST `
        -ContentType "application/x-www-form-urlencoded" `
        -Body $body `
        -ErrorAction Stop

    Write-Host "✅ 教师登录成功" -ForegroundColor Green
    Write-Host "教师信息:" -ForegroundColor Gray
    Write-Host "  工号: $($response.data.teNo)" -ForegroundColor Gray
    Write-Host "  姓名: $($response.data.teName)" -ForegroundColor Gray
} catch {
    $statusCode = $_.Exception.Response.StatusCode.value__
    Write-Host "❌ 教师登录失败 (状态码: $statusCode)" -ForegroundColor Red
    Write-Host "错误信息: $($_.Exception.Message)" -ForegroundColor Yellow
}
Write-Host ""

Write-Host "============================================" -ForegroundColor Cyan
Write-Host "测试完成！" -ForegroundColor Cyan
Write-Host "============================================" -ForegroundColor Cyan
Write-Host ""
Write-Host "如果以上测试通过，说明后端服务正常运行。" -ForegroundColor Green
Write-Host "现在可以启动前端开发服务器：" -ForegroundColor Green
Write-Host "  npm run dev" -ForegroundColor Yellow
Write-Host ""
Write-Host "或者部署到服务器：" -ForegroundColor Green
Write-Host "  bash deploy.sh" -ForegroundColor Yellow
Write-Host ""

Read-Host "按回车键退出"












